#! /bin/bash -e

#####################################################################
#                                                                   #
#               Compiles several Faust programs to JACK-QT          #
#               (c) Grame, 2018                                     #
#                                                                   #
#####################################################################

. faustpath
. faustoptflags
. usage.sh

CXXFLAGS+=" $MYGCCFLAGS"  # So that additional CXXFLAGS can be used

ARCHFILE=$FAUSTARCH/jack-qt-chain-header.cpp

#-------------------------------------------------------------------
# Set Faust include path

ARCHLIB="`pkg-config --cflags --libs jack`"

echoHelp()
{
    usage faust2jaqtchain "[options] [Faust options] <file.dsp>"
    require Jack, QT
    echo "Compiles several Faust programs to JACK and QT"
    option
    options -httpd -osc
    option "Faust options"
    exit
}

if [ "$#" -eq 0 ]; then
    echo 'Please, provide a Faust file to process !'
    echo ''
    echoHelp
fi

#-------------------------------------------------------------------------------
# Search for qmake or qmake-qt4

QMAKE=$(which qmake-qt5 || which qmake-qt4 || echo qmake)

#PHASE 2 : dispatch command arguments

QTDEFS=""

OSCINC=""
OSCLIB=""
HTTPDEFS=""
HTTPLIBS=""
DEPLOY=""

while [ $1 ]
do
    p=$1

    if [ $p = "-help" ] || [ $p = "-h" ]; then
        echoHelp
    elif [ "$p" = -icc ]; then
        ignore=" "
    elif [ $p = "-deploy" ]; then
        DEPLOY="yes"
    elif [ $p = "-osc" ]; then
        OSCDEFS="DEFINES += OSCCTRL"
        OSCLIBS="-lOSCFaust"
    elif [ "$p" = "-httpd" ]; then
        HTTPDEFS="DEFINES += HTTPCTRL"
        HTTPLIBS="-lHTTPDFaust"
        HTTPLIBS1=`pkg-config --cflags --libs libmicrohttpd`
    elif [ ${p:0:1} = "-" ]; then
        OPTIONS="$OPTIONS $p"
    elif [[ -f "$p" ]] && [ ${p: -4} == ".dsp" ]; then
        FILES="$FILES $p"
    else
        OPTIONS="$OPTIONS $p"
    fi

shift

done

#-------------------------------------------------------------------
# Check Darwin specifics
#
if [[ $(uname) == Darwin ]]; then
    ARCHLIB+=" -framework CoreMIDI -framework CoreFoundation -framework CoreAudio -framework AudioUnit -framework CoreServices"
    SYS_VERSION=$(uname -v | cut -d : -f1 | cut -d. -f1 | cut -d' ' -f4)
    if [ $SYS_VERSION -ge 13 ]
    then
        SPEC="-spec macx-clang"
        CLANGOPT="QMAKE_MACOSX_DEPLOYMENT_TARGET = 10.9"
        CXXFLAGS="$CXXFLAGS -mmacosx-version-min=10.9"
    else
        SPEC="-spec macx-g++"
        CLANGOPT=""
    fi
    EXT=".app"
fi

#-------------------------------------------------------------------
# Check Linux specifics
#
if [[ $(uname) == Linux ]]; then
    ARCHLIB+=" -lasound"
fi

# creates a temporary dir
TDR=$(mktemp -d faust.XXXXXX)
TMP="$TDR/chain"
mkdir "$TMP"

#-------------------------------------------------------------------
cat $ARCHFILE > "$TMP/combined.cpp"
echo "void addAllDSP() {" >> "$TMP/combined_dsp.cpp"

#-------------------------------------------------------------------
# compile the *.dsp files
#
for p in $FILES; do

    CUR=$(pwd)
    f=$(basename "$p")
    SRCDIR=$(dirname "$p")
    name=$(basename "$f" .dsp)

    echo $f

    # compile faust to c++
     faust $OPTIONS "$SRCDIR/$f" -cn $name -o "$TMP/${f%.dsp}.cpp" || exit

    # push DSP in the chain
    cat "$TMP/${f%.dsp}.cpp" >> "$TMP/combined.cpp"
    echo "dsp_chain.push_back(std::make_pair(\"$name\", new $name()));" >> "$TMP/combined_dsp.cpp"
    rm "$TMP/${f%.dsp}.cpp"

done

#-------------------------------------------------------------------
echo "}" >> "$TMP/combined_dsp.cpp"
cat "$TMP/combined_dsp.cpp" >> "$TMP/combined.cpp"
cat $FAUSTARCH/jack-qt-chain-footer.cpp >> "$TMP/combined.cpp"
rm "$TMP/combined_dsp.cpp"

# compile c++ to binary
(
    cd "$TMP"
    $QMAKE -project "QT += widgets printsupport network" "CONFIG+=warn_off" "$CLANGOPT" "INCLUDEPATH+=$CUR" "INCLUDEPATH+=$FAUSTINC" "QMAKE_CXXFLAGS=$CXXFLAGS -Wno-unused-parameter $FAUSTTOOLSFLAGS" "LIBS+=$ARCHLIB $SOUNDFILELIBS $OSCLIBS $HTTPLIBS $HTTPLIBS1" "HEADERS+=$FAUSTINC/faust/gui/QTUI.h" "RESOURCES+= $FAUSTINC/faust/gui/Styles/Grey.qrc" "$OSCDEFS" "$HTTPDEFS" "$QRDEFS" "$POLYDEFS" "$MIDIDEFS" "$SOUNDFILEDEFS"
    $QMAKE $SPEC QMAKE_CFLAGS_ISYSTEM=-I
    make
) > /dev/null || exit

cp -a "$TMP/chain$EXT" "$SRCDIR/chain$EXT"
if [[ $(uname) == Darwin ]]; then
    codesign --sign - --deep --force "$SRCDIR/chain$EXT"
fi
rm -rf "$TDR"

# collect binary file name for FaustGIDE
BINARIES="$BINARIES$SRCDIR/chain$EXT;"

echo $BINARIES

